package com.augurit.wf.app.notice.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.wf.app.notice.convert.NoticeConverter;
import com.augurit.wf.app.notice.service.INoticeService;
import com.augurit.wf.app.notice.web.form.NoticeForm;
import com.augurit.wf.common.dao.NoticeDao;
import com.augurit.wf.common.entity.Notice;
import com.augurit.wf.common.service.impl.WfBusClientServiceImpl;

/**
 * 公告板实现类
 */
@Service
@Transactional
public class NoticeServiceImpl extends WfBusClientServiceImpl<NoticeForm> implements INoticeService {

	@Autowired
	private NoticeDao noticeDao;

	/**
	 * 删除公告列表
	 * @param 公告id数组
	 */
	public void delete(Long... ids) {
		// TODO Auto-generated method stub
		if(ids != null)
			for(Long id : ids)
				noticeDao.delete(id);
	}
	
	/**
	 * 获取公告form对象
	 * @param id 公告id
	 * @return
	 */
	@Transactional(readOnly = true)
	public NoticeForm get(Long id) {
		NoticeForm form = noticeDao.getForm(id);		
		return form;
	}
	
	/**
	 * 根据分页对象和过滤条件列表进行分页条件查询
	 * @param page 分页对象
	 * @param form 查询条件
	 * @return 分页查询结果.附带结果列表及所有查询时的参数
	 */
	@Transactional(readOnly = true)
	public Page<NoticeForm> search(Page<NoticeForm> page, NoticeForm form){
		
		// 建立新的分页对象
		Page<Notice> pg = PageUtils.newPageInstance(page);
		
		StringBuffer hql = new StringBuffer("from Notice ps where 1=1 ");
		List values = new ArrayList();
		
		if(form != null){
			if(StringUtils.isNotBlank(form.getTitle())){
				hql.append(" and ps.title like ?");
				values.add("%" + form.getTitle() + "%");
			}
			if(StringUtils.isNotBlank(form.getPublisher())){
				hql.append(" and ps.publisher like ?");
				values.add("%" + form.getPublisher() + "%");
			}
			if(StringUtils.isNotBlank(form.getContent())){
				hql.append(" and ps.content like ?");
				values.add("%" + form.getContent() + "%");
			}
			if(form.getPublishTime() != null){
				hql.append(" and ps.publishTime = ?");
				values.add(form.getPublishTime());
			}
		}
		
		// 按过滤条件分页查找对象
		pg = noticeDao.findPage(pg, hql.toString(),values);
		
		List<NoticeForm> list = NoticeConverter.convertToFormList(pg.getResult());
		// 转换为Form对象列表并赋值到原分页对象中
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 获取系统中所有的公告列表
	 * @return 公告form列表
	 */
	@Transactional(readOnly = true)
	public List<NoticeForm> getAll(){
		List<Notice> list = noticeDao.getAll();
		return NoticeConverter.convertToFormList(list);
	}

	public void save(NoticeForm... forms) {
		if(forms != null)
			for(NoticeForm form : forms)
				noticeDao.save(form);
	}

	public void save(NoticeForm form) {
		if(form != null)
			noticeDao.save(form);
	}
}
